package com.chwn.harulib.util;

import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by Myshikin on 2017/3/28.
 */

public class ToastUtil {
    private static final String  CHECK_OP_NO_THROW    = "checkOpNoThrow";
    private static final String  OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
    private static       long    time                 = 2000L;
    private static       long    showTime             = 0L;
    private static       int     layout_id            = 0;
    private static       int     text_id              = 0;
    private static       boolean sysEnable            = true;
    private static Application ap;

    public ToastUtil() {
    }

    public static void setLayoutId(int layout_id, int text_id) {
        layout_id = layout_id;
        text_id = text_id;
    }

    public static void setIntervalTime(long _time) {
        time = _time;
    }

    public static void init(Application _ap) {
        ap = _ap;
        check();
    }

    public static void shortShowCustom(int resId) {
        shortShowCustom(resId, (String) null, 17, 0, 0);
    }

    public static void shortShowCustom(String content) {
        shortShowCustom(content, 17, 0, 0);
    }

    public static void shortShowCustom(int resId, int gravity, int xOffset, int yOffset) {
        shortShowCustom(resId, (String) null, gravity, xOffset, yOffset);
    }

    public static void shortShowCustom(String content, int gravity, int xOffset, int yOffset) {
        shortShowCustom(-1, content, gravity, xOffset, yOffset);
    }

    private static void shortShowCustom(int resId, String content, int gravity, int xOffset, int yOffset) {
        if (ap != null) {
            if (layout_id > 0) {
                if (isShow()) {
                    return;
                }

                LayoutInflater inflater = LayoutInflater.from(ap);
                View layout = inflater.inflate(layout_id, (ViewGroup) null);
                TextView text = (TextView) layout.findViewById(text_id);
                if (resId < 0) {
                    text.setText(content);
                } else {
                    text.setText(resId);
                }

                if (sysEnable) {
                    Toast toast = new Toast(ap);
                    toast.setGravity(gravity, xOffset, yOffset);
                    toast.setDuration(Toast.LENGTH_SHORT);
                    toast.setView(layout);
                    toast.show();
                } else {
                    CustomToast.makeText(ap, layout, 0L).show();
                }
            } else {
                showToast(content, resId, true);
            }

        }
    }

    public static void shortShowToast(String content) {
        showToast(content, -1, true);
    }

    public static void shortShowToast(int resId) {
        showToast((String) null, resId, true);
    }

    public static void shortShowLogin() {
        showToast("请先登录", -1, true);
    }

    public static void longShowToast(String content) {
        showToast(content, -1, false);
    }

    public static void longShowToast(int resId) {
        showToast((String) null, resId, false);
    }

    private static void showToast(String content, int resId, boolean isShort) {
        if (ap != null && !isShow()) {
            if (sysEnable) {
                Toast.makeText(ap, resId > 0 ? ap.getResources().getString(resId) : content, isShort ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG).show();
            } else {
                CustomToast.makeText(ap, resId > 0 ? ap.getResources().getString(resId) : content, (long) (isShort ? 0 : 1)).show();
            }

        }
    }

    private static boolean isShow() {
        if (System.currentTimeMillis() - showTime > time) {
            showTime = System.currentTimeMillis();
            return false;
        } else {
            return true;
        }
    }

    private static boolean check() {
        if (Build.VERSION.SDK_INT >= 19) {
            return sysEnable = isNotificationEnabled(ap);
        } else {
            sysEnable = true;
            return true;
        }
    }

    @TargetApi(19)
    public static boolean isNotificationEnabled(Context context) {
        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService("appops");
        ApplicationInfo appInfo = context.getApplicationInfo();
        String pkg = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;
        Class appOpsClass = null;

        try {
            appOpsClass = Class.forName(AppOpsManager.class.getName());
            Method e = appOpsClass.getMethod("checkOpNoThrow", new Class[]{Integer.TYPE, Integer.TYPE, String.class});
            Field opPostNotificationValue = appOpsClass.getDeclaredField("OP_POST_NOTIFICATION");
            int value = ((Integer) opPostNotificationValue.get(Integer.class)).intValue();
            return ((Integer) e.invoke(mAppOps, new Object[]{Integer.valueOf(value), Integer.valueOf(uid), pkg})).intValue() == 0;
        } catch (Exception var9) {
            var9.printStackTrace();
            return true;
        }
    }
}
